Handle exceptions from server callbacks #337
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In its current form, when a server callback throws an exception, it is completely swallowed. Only when the asyncio loop is being shut down might one possibly see that error. On top of that, the connection is never closed, causing any clients to hang, and a memory leak in the server.
This is a proposed fix that reports the exception to the asyncio exception handler. It also makes sure that the connection is always closed, even if the callback doesn't close it explicitly.
Note that the design of AsyncIoStream is directly based on the design of Python's asyncio streams: https://docs.python.org/3/library/asyncio-stream.html These streams appear to have exactly the same flaw. I've reported this here: python/cpython#110894. Since I don't really know what I'm doing, it might be worth seeing what kind of solution they might come up with and model our solution after theirs.